//Spiny From CPZ
freeslot("MT_SPINY")

local function P_ParabolicMove(actor, target_x, target_y, target_z, speed)
local x = target_x
local y = target_y
local z = target_z
local dh
x = $1 - actor.x
y = $1 - actor.y
z = $1 - actor.z
dh = P_AproxDistance(x, y)
actor.momx = FixedMul(FixedDiv(x, dh), speed)
actor.momy = FixedMul(FixedDiv(y, dh), speed)
	if not gravity
	return
	end
dh = FixedDiv(FixedMul(dh, gravity), speed)
actor.momz = (dh>>1) + FixedDiv(z, dh<<1)
end

addHook("MobjThinker", function(mo)
	if mo
	and mo.valid
	and mo.type == MT_SPINY
		if mo.health
		A_FindTarget(mo, MT_PLAYER, 0)
		mo.spritexscale = mo.scale+FRACUNIT
		mo.spriteyscale = mo.scale+FRACUNIT
			if mo.spiny_cooldown == nil
			mo.spiny_cooldown = 0
			end
			if mo.state == S_SPINY_WALK1
			or mo.state == S_SPINY_WALK2
			or mo.state == S_SPINY_WALK3
			or mo.state == S_SPINY_WALK4
			P_InstaThrust(mo, mo.angle, FRACUNIT)
				if mo.reactiontime > 0
				mo.reactiontime = $1 - 1
				elseif mo.reactiontime <= 0
				mo.angle = $1 + ANGLE_180
				mo.reactiontime = 3*TICRATE
				end
				if mo.spiny_cooldown > 0
				mo.spiny_cooldown = $1 - 1
				end
				if mo.target
				and mo.target.valid
					if R_PointToDist2(mo.x, mo.y, mo.target.x, mo.target.y) <= 256*FRACUNIT
					and mo.state == S_SPINY_WALK1
					and P_CheckSight(mo, mo.target)
					and mo.spiny_cooldown <= 0
					mo.spiny_cooldown = TICRATE+15
					mo.state = S_SPINY_SHOT1
					end
				end
			elseif mo.state == S_SPINY_SHOT1
			or mo.state == S_SPINY_SHOT2
			or mo.state == S_SPINY_SHOT3
			or mo.state == S_SPINY_SHOT4
			mo.momx = 0
			mo.momy = 0
			end
			if mo.state == S_SPINY_SHOT2
				if mo.tics == 2
					if mo.target
					and mo.target.valid
					//local bullet = P_SpawnMobj(mo.x, mo.y, mo.z+mo.height+4*FRACUNIT, MT_JETTBULLET)
					S_StartSound(mo, sfx_s3k4d)
					local bullet = P_SpawnMobjFromMobj(mo, 0, 0, mo.height+4*FRACUNIT, MT_JETTBULLET)
						if bullet
						and bullet.valid
						bullet.flags = $1 & ~MF_NOGRAVITY
						bullet.flags2 = $1 & ~MF2_OBJECTFLIP
						bullet.target = mo
							if (mo.eflags & MFE_VERTICALFLIP)
							P_ParabolicMove(bullet, mo.target.x, mo.target.y, mo.target.z, 16*FRACUNIT)
							else
							P_SetObjectMomZ(bullet, 6*FRACUNIT)
							P_InstaThrust(bullet, R_PointToAngle2(mo.x, mo.y, mo.target.x, mo.target.y), 8*FRACUNIT)
							end
						end
					end
				end
			end
		end
	end
end, MT_SPINY)

addHook("MobjMoveBlocked", function(mo)
	if mo
	and mo.valid
	and mo.type == MT_SPINY
	and mo.health
		if mo.reactiontime > 2
		mo.reactiontime = 2
		end
	end
end, MT_SPINY)

addHook("MobjDeath", function(mo)
mo.spritexscale = mo.scale
mo.spriteyscale = mo.scale
end, MT_SPINY)